All files / web/src/app/api/classrooms/[classroomId]/presence route.ts

0% Statements 0/94
0% Branches 0/1
0% Functions 0/1
0% Lines 0/94

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95                                                                                                                                                                                             
import { NextResponse } from 'next/server'
import {
  enterClassroom,
  getClassroomPresence,
  getTeacherClassroom,
  isParentOf,
} from '@/lib/classroom'
import { getUserId } from '@/lib/viewer'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * GET /api/classrooms/[classroomId]/presence
 * Get all students currently present in classroom (teacher only)
 *
 * Returns: { students: Player[] }
 */
export const GET = withAuth(async (_request, { params }) => {
  try {
    const { classroomId } = (await params) as { classroomId: string }
    const userId = await getUserId()

    // Verify user is the teacher of this classroom
    const classroom = await getTeacherClassroom(userId)
    if (!classroom || classroom.id !== classroomId) {
      return NextResponse.json({ error: 'Not authorized' }, { status: 403 })
    }

    const presences = await getClassroomPresence(classroomId)

    // Return players with presence info
    return NextResponse.json({
      students: presences.map((p) => ({
        ...p.player,
        enteredAt: p.enteredAt,
        enteredBy: p.enteredBy,
      })),
    })
  } catch (error) {
    console.error('Failed to fetch classroom presence:', error)
    return NextResponse.json({ error: 'Failed to fetch classroom presence' }, { status: 500 })
  }
})

/**
 * POST /api/classrooms/[classroomId]/presence
 * Enter student into classroom (teacher or parent)
 *
 * Body: { playerId: string }
 * Returns: { success: true, presence } or { success: false, error }
 */
export const POST = withAuth(async (req, { params }) => {
  try {
    const { classroomId } = (await params) as { classroomId: string }
    const userId = await getUserId()
    const body = await req.json()

    if (!body.playerId) {
      return NextResponse.json({ success: false, error: 'Missing playerId' }, { status: 400 })
    }

    // Check authorization: must be teacher of classroom OR parent of student
    const classroom = await getTeacherClassroom(userId)
    const isTeacher = classroom?.id === classroomId
    const parentCheck = await isParentOf(userId, body.playerId)

    if (!isTeacher && !parentCheck) {
      return NextResponse.json(
        {
          success: false,
          error: 'Must be the classroom teacher or a parent of the student',
        },
        { status: 403 }
      )
    }

    const result = await enterClassroom({
      playerId: body.playerId,
      classroomId,
      enteredBy: userId,
    })

    if (!result.success) {
      return NextResponse.json({ success: false, error: result.error }, { status: 400 })
    }

    return NextResponse.json({ success: true, presence: result.presence })
  } catch (error) {
    console.error('Failed to enter classroom:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to enter classroom' },
      { status: 500 }
    )
  }
})